<?php
declare (strict_types=1);

namespace app\command\company;

use app\company\model\Store;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\company\model\StoreSalesman as StoreSalesmanModel;

class StoreSalesman extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('int:store_salesman')
             ->setDescription('初始化业务员门店关联关系');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            // 指令输出
            Store::where('status', 1)
                 ->field('id, salesman_id as store_user_id')
                 ->select()
                 ->each(function ($item) {
                     $row = StoreSalesmanModel::where('store_id', $item->id)->where('store_user_id',$item->store_user_id)->find();
                     if(!$row){
                         StoreSalesmanModel::create([
                                                        'store_id'      => $item->id,
                                                        'store_user_id' => $item->store_user_id
                                                    ]);
                     }
                 });
        }catch (\Throwable $e){
            $output->writeln($e->getMessage());
        }
       $output->writeln('执行成功');
    }
}
